
import datetime
from django.shortcuts import render
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from activity.serializers import GatheringSerializer, GatheringDSerializers
from activity.models import Gathering
from activity.pages import PageNum

# Create your views here.

# 活动列表
class GatheringSimpleView(ListAPIView):
    queryset = Gathering.objects.all().order_by('id')
    serializer_class = GatheringSerializer
    pagination_class = PageNum

# 活动详情
class GatheringDetailView(RetrieveAPIView):
    queryset = Gathering.objects.all()
    serializer_class = GatheringDSerializers

# 活动报名
class GatheringJoinView(APIView):

    def post(self, request, pk):
        gathering = Gathering.objects.get(id=pk)
        now_datetime = datetime.datetime.now()
        if now_datetime > gathering.endrolltime:
            return Response({'success': False, 'message': '来晚了亲'})
        user = request.user
        if user in gathering.users.all():
            gathering.users.remove(user)
            return Response({'success': True, 'message': '取消成功'})
        gathering.users.add(user)
        return Response({'success': True, 'message': '报名成功'})












